//
//  XYVisitorLoginView.swift
//  01.XinLangWB
//
//  Created by xiaoyudong on 16/4/8.
//  Copyright © 2016年 苏小样. All rights reserved.
//

import UIKit
import SnapKit
@objc protocol XYVisitorLoginViewDelegate: NSObjectProtocol{
    optional func userWillLogin()
    optional func userWillRegister()
    
}
class XYVisitorLoginView: UIView {
    //声明代理对象  需要考虑循环引用
    //swift 中属性默认的修饰 都是strong
   weak var visitorDelegate: XYVisitorLoginViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //对外提供和设置界面的方法
    func setVisitorInfo(tipText:String, imageName: String ,isHome: Bool = false){
        if isHome{
            startAnimation()
        }else{
            //隐藏
            largeHouse.hidden = true
            //为了解决一部分的图片被盖住的问题
            //把 圈圈视图移动到最顶部
            bringSubviewToFront(cirecleIcon)
//            backImageView.hidden = true
        }
        tipLabel.text = tipText
        cirecleIcon.image = UIImage(named: imageName)
        
    }
    //设置动画
    private func startAnimation(){
        //基本动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //基本设置
        anim.repeatCount = MAXFLOAT
        //动画时间
        anim.duration = 15
        anim.toValue = 2 * M_PI
        // 动画执行结束 或者 当前处于非活跃状态下 动画不移除图层
        anim.removedOnCompletion = false
        cirecleIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    //MARK:设置UI页面
   private func setupUI() {
    addSubview(cirecleIcon)
    addSubview(backImageView)
    addSubview(largeHouse)
    addSubview(tipLabel)
    addSubview(loginBtn)
    addSubview(registerBtn)
    cirecleIcon.snp_makeConstraints { (make) -> Void in
        //make 就是 circleIcon 取消了frame布局的对象
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY).offset(-80)
        
    }
    largeHouse.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(cirecleIcon.snp_center)
    }
    tipLabel.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(cirecleIcon.snp_bottom).offset(16)
        //为了能够自动换行 需要约束宽度
        make.width.equalTo(230)
        //设置高度 解决按钮跳动问题
        make.height.equalTo(40)
        //设置高度约束
        make.centerX.equalTo(cirecleIcon.snp_centerX)
    }
    loginBtn.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(tipLabel.snp_bottom).offset(16)
        make.left.equalTo(tipLabel.snp_left)
        //设置约束宽度
        make.width.equalTo(100)
    }
    registerBtn.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(tipLabel.snp_bottom).offset(16)
        make.right.equalTo(tipLabel.snp_right)
        //设置约束宽度
        make.width.equalTo(100)
    }
    backImageView.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(cirecleIcon.snp_center)
    }
        //设置背景颜色
    let value: CGFloat = 237 / 255.0
    backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
    //添加点击事件
    loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
    registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    //MARK: 按钮的监听事件
 @objc   private func registerBtnDidClick(){
    print("注册")
        visitorDelegate?.userWillRegister?()
    }
  @objc  private func loginBtnDidClick(){
    print("登录")
        visitorDelegate?.userWillLogin?()
    }
   //懒加载子视图
    //背景视图
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    private lazy var cirecleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
  //大房子
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提交文案
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人,回到这里喜,关注一些人,回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        //自动换行
        l.numberOfLines = 0
        //居中
        l.textAlignment = .Center
        //自动适应
        l.sizeToFit()
        return l
    }()
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let b = UIButton()
        b.setTitle("登录", forState: .Normal)
        b.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        b.titleLabel?.font = UIFont.systemFontOfSize(14)
        //背景
       let img = UIImage(named: "common_button_white_disable")!
        let capW = Int(img.size.width * 0.5)
        let capH = Int(img.size.height * 0.5)
        b.setBackgroundImage(img.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        return b
    }()
    //注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn

    }()
    
}
